﻿using NCKH.models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NCKH.Controllers
{
    public class PLAN_HocKyDangKy_TCCtrl
    {
        public static void LoadDataSource(ComboBox comboBox, PLAN_HocKyDangKy_TC value = null, Context db = null)
        {
            if (db == null) db = new Context();

            var values = db.PLAN_HocKyDangKy_TCs.Select(s => s.Nam_hoc).Distinct().OrderBy(o => o).ToList();

            comboBox.DataSource = values;

            if (value == null)
            {
                var date = string.Empty;

                if (DateTime.Now.Month >= 8)
                {
                    date = String.Format("{0}-{1}", DateTime.Now.Year, (DateTime.Now.Year + 1));
                }
                else
                {
                    date = String.Format("{0}-{1}", (DateTime.Now.Year - 1), (DateTime.Now.Year));
                }
                value = new PLAN_HocKyDangKy_TC { Nam_hoc = date };
            }

            comboBox.SelectedIndex = value == null ? 0 : values.IndexOf(value.Nam_hoc);
        }

        public static void LoadDataSource(ComboBox comboBox, string key, PLAN_HocKyDangKy_TC value = null, Context db = null)
        {
            if (db == null) db = new Context();

            var values = db.PLAN_HocKyDangKy_TCs.Where(w => w.Nam_hoc == key).Select(s => new { s.Hoc_ky, s.Dot, s.Ky_phu }).Distinct().OrderBy(o => o.Hoc_ky).ThenBy(o => o.Dot).ToList();
            
            var items = new List<string>();

            foreach (var item in values)
            {
                if (item.Ky_phu == true) items.Add("1A");
                else
                    if (item.Hoc_ky == 1) items.Add("1B");
                    else
                        if (item.Hoc_ky == 2) items.Add("2");
            }

            comboBox.Items.Clear();

            items = items.Distinct().ToList();

            foreach (var item in items)
            {
                comboBox.Items.Add(item);
            }

            if (value != null)
            {
                comboBox.SelectedIndex = value == null ? 0 : items.IndexOf((value.Ky_phu == true ? "1A" : value.Hoc_ky == 1 ? "1B" : "2"));
            }
            else
            {
                var hocKy = DateTime.Now.Month >= 8 ? 1 : 2 ;
                comboBox.SelectedIndex = hocKy == 2 ? items.IndexOf("2") == -1 ? 0 : items.IndexOf("2") : 0;
            }
        }

        public static void LoadDataSource(ComboBox comboBox, string key1, string key2, PLAN_HocKyDangKy_TC value = null, Context db = null)
        {
            if (db == null) db = new Context();

            var items = new List<int>();

            if (key2 == "1A")
            {
                items.Add(1);
            }
            else
            {
                var hocKy = key2 == "2" ? 2 : 1;

                var values = db.PLAN_HocKyDangKy_TCs.Where(w => w.Nam_hoc == key1 && w.Hoc_ky == hocKy && w.Ky_phu != true).Select(s => s.Dot).Distinct().OrderBy(o => o).ToList();

                items.AddRange(values);
            }

            comboBox.Items.Clear();

            items = items.Distinct().ToList();

            foreach (var item in items)
            {
                comboBox.Items.Add(item);
            }

            comboBox.SelectedIndex = value == null || items.Count == 1 ? 0 : items.IndexOf(value.Dot);
        }
        public static void LoadDataSource(ComboBox comboBox, string key, string key1, string key2, View_TaoDanhSach_ThongTinMonHocDK_1 value = null, Context db = null)
        {
            if (db == null) db = new Context();
            var a = db.View_TaoDanhSach_ThongTinMonHocDK_1s.Where(p => p.Nam_hoc == key && p.Hoc_ky == (key1 == "2" ? 2 : 1) && p.Dot == int.Parse(key2)).Select(o => o.Ten_he).Distinct().ToList();
            comboBox.Items.Clear();
            foreach (var item in a)
            {
                comboBox.Items.Add(item);
            }
            comboBox.SelectedIndex = value == null || comboBox.Items.Count > 0 ? 0 : comboBox.Items.IndexOf(value.Ten_he);
        }

        public static PLAN_HocKyDangKy_TC GetItem(string namHoc, string hocKy, string dot, Context db = null)
        {
            if (db == null) db = new Context();

            var Hoc_ky = hocKy == "2" ? 2 : 1;
            var Dot = int.Parse(dot);

            return db.PLAN_HocKyDangKy_TCs.SingleOrDefault(w => w.Nam_hoc == namHoc && w.Hoc_ky == Hoc_ky && w.Dot == Dot);
        }

        public static int FindBeginWeek(List<SYS_ThamSoHeThongEX> thamSos, DateTime Tu_ngay, DateTime? Den_ngay)
        {
            var thamSo = thamSos.SingleOrDefault(s => s.Ma_tham_so == "Ngay_bat_dau_nam_hoc");
            var begin = thamSo == null ? (new DateTime(2013, 08, 01)) : DateTime.Parse(thamSo.Gia_tri);

            int year = Tu_ngay < (new DateTime(Tu_ngay.Year, begin.Month, begin.Day)) ? Tu_ngay.Year - 1 : Tu_ngay.Year;

            int dayOfWeek = (int)(new DateTime(year, begin.Month, begin.Day)).DayOfWeek;
            dayOfWeek = dayOfWeek == 0 ? 7 : dayOfWeek;
            DateTime time = (new DateTime(year, begin.Month, begin.Day)).AddDays(1 - dayOfWeek);

            int week = 0;

            if (time.AddDays(7) >= Tu_ngay) return 1;

            while ((time = time.AddDays(7)) < Tu_ngay)
            {
                week++;
            }

            return week;
        }

        public static int FindDayOf(DateTime ngay_bat_dau, DateTime ngay_tim_kiem, int thu)  //Thứ 2 = 0 .... Chủ nhật  = 6
        {
            var index = 0;

            thu = (thu == 6) ? 0 : thu + 1;    //DayOfWeek Chủ nhật = 0 .... Thứ 7 = 6

            if ((int)ngay_bat_dau.DayOfWeek > thu)
            {
                index = 7 - ((int)ngay_bat_dau.DayOfWeek - thu);
                ngay_bat_dau = ngay_bat_dau.AddDays(7 - ((int)ngay_bat_dau.DayOfWeek - thu));
            }
            else if ((int)ngay_tim_kiem.DayOfWeek < thu)
            {
                index = (thu - (int)ngay_bat_dau.DayOfWeek);
                ngay_bat_dau = ngay_bat_dau.AddDays((thu - (int)ngay_bat_dau.DayOfWeek));
            }

            ngay_bat_dau = new DateTime(ngay_bat_dau.Year, ngay_bat_dau.Month, ngay_bat_dau.Day);
            

            if ((int)ngay_tim_kiem.DayOfWeek > thu) 
            {
                ngay_tim_kiem = ngay_tim_kiem.AddDays(-1 * (7 - ((int)ngay_bat_dau.DayOfWeek - thu)));
            }
            else if ((int)ngay_tim_kiem.DayOfWeek < thu) 
            {
                ngay_tim_kiem = ngay_tim_kiem.AddDays(-1 * (thu - (int)ngay_tim_kiem.DayOfWeek));
            }

            ngay_tim_kiem = new DateTime(ngay_tim_kiem.Year, ngay_tim_kiem.Month, ngay_tim_kiem.Day);

            while (ngay_bat_dau < ngay_tim_kiem)
            {
                if (ngay_bat_dau.Month == ngay_tim_kiem.Month && ngay_bat_dau.Year == ngay_tim_kiem.Year)
                {
                    index++;
                    ngay_bat_dau = ngay_bat_dau.AddDays(1);
                }
                else if (ngay_bat_dau.Month < ngay_tim_kiem.Month && ngay_bat_dau.Year == ngay_tim_kiem.Year)
                {
                    index += DateTime.DaysInMonth(ngay_bat_dau.Year, ngay_bat_dau.Month) - ngay_bat_dau.Day + 1;
                    ngay_bat_dau = ngay_bat_dau.AddDays(DateTime.DaysInMonth(ngay_bat_dau.Year, ngay_bat_dau.Month) - ngay_bat_dau.Day + 1);
                }
                else if (ngay_bat_dau.Year < ngay_tim_kiem.Year)
                {
                    index += (DateTime.IsLeapYear(ngay_bat_dau.Year) ? (int)366 : (int)365) - ngay_bat_dau.DayOfYear + 1;
                    ngay_bat_dau = ngay_bat_dau.AddDays((DateTime.IsLeapYear(ngay_bat_dau.Year) ? (int)366 : (int)365) - ngay_bat_dau.DayOfYear + 1);
                }
            }

            return index;
        }
    }
}
